
package com.probiz.estoresa.system.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.common.model.customer.Membership;
import com.probiz.estore.common.model.system.AppRole;
import com.probiz.estore.core.controller.GenericStoreAdminController;
import com.probiz.estore.core.exception.ApplicationException;
import com.probiz.estore.customer.service.MembershipManager;
import com.probiz.estore.system.service.AppRoleManager;

public class FrontMembershipController extends GenericStoreAdminController<Membership> {
	private MembershipManager	membershipManager	= null;
	private AppRoleManager	appRoleManager = null;
	/*spring-mvc.xml中配置值*/
    private Short belongTo = null;
    public void setBelongTo(Short belongTo) {
		this.belongTo = belongTo;
	}

	public void setMembershipManager(MembershipManager membershipManager) {
		this.membershipManager = membershipManager;
	}
	
	public void setAppRoleManager(AppRoleManager appRoleManager) {
		this.appRoleManager = appRoleManager;
	}

	@Override
	protected String getEntityName(Membership entity) {
		return entity.getMembershipName();
	}


	@Override
	protected void initController() throws Exception {
		this.mgr = membershipManager;
	}

	@Override
	protected void preSave(HttpServletRequest request, Membership entity, BindException errors) {
	}
	
	
	@Override
	public ModelAndView save(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 取得Form对应的Model
		Membership entity = formBackingObject(request);
		BindException errors = null;
		try {
			ServletRequestDataBinder binder = bindAndValidate(request, entity);
			errors = new BindException(binder.getBindingResult());
			
		} catch (ApplicationException e) {
			handleApplicationException(errors, e);
		}
		//set the roles for current appadmin
		entity.getAppRoles().clear();
        String[] appRoleIds=request.getParameterValues("appRoleIds");
        if(appRoleIds!=null){
            for(int i=0;i<appRoleIds.length;i++){
            	AppRole role=appRoleManager.getById(new Integer(appRoleIds[i]));
            	entity.getAppRoles().add(role);
            }
        }
        membershipManager.save(entity);
        
        //String msgKey = (isEntityNew(request)) ? "common.added" : "common.updated";
		//String message = getMessage(msgKey, new Object[] { getEntityTypeMessage(), getEntityName(entity) });
        String message = "对于会员等级["+getEntityName(entity)+"]分配角色成功!";
        saveMessage(request, message);
		return showForm(request, errors);
	}
	@Override
	protected void onShowForm(HttpServletRequest request, ModelAndView mv) {
		mv.addObject("allRoles",appRoleManager.findAppRoles(this.belongTo));
		super.onShowForm(request, mv);
	}
}
